<template>
  <div class="w h box-b p8 b-f">
    <div class="w m-b2">
      <el-button type="primary" @click="getDataList">查询</el-button>
      <el-button type="primary" @click="appendTeam()">创建团队</el-button>
    </div>
    <div style="height:calc(100% - 60px)" class="w">
      <el-table :data="tableData" row-key="uniId" height="100%" border default-expand-all>
        <el-table-column prop="name" label="姓名" width="250" sortable>
          <template #default="{ row }">
            <span v-if="!row.isChildren" class="c8">团队名称：</span>
            <el-icon-render color="#777" class="el-icon-UserFilled" style="margin-right:4px"
              v-if="row.isChildren"></el-icon-render>
            <span>
              {{ row.name }}</span>
          </template>

        </el-table-column>
        <el-table-column prop="type" label="工号" width="250" sortable>
          <template #default="{ row }">
            <span v-if="!row.isChildren"> <span class="c8">类型：</span>
              {{ ['标注', '审批', '清洗'][row.type] }}</span>
            <span v-else>{{ row.jobNumber }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="explain" label="详细信息" sortable>
          <template #default="{ row }">
            <span v-if="!row.isChildren"><span class="c8">描述:</span>{{ row.explain }}</span>
            <div class="f-s a-i-c" v-else>
              <span class="p-r4 u-s-t w-160"><span class="c8">手机号：</span>{{ row.mobile }}</span>
              <span class="p-r4 u-s-t w-200"><span class="c8">邮箱：</span>{{ row.email }}</span>
              <span class="p-r4 u-s-t w-160"><span class="c8">用户名：</span>{{ row.username }}</span>
              <span class="p-r4 u-s-t w-260" v-if="row.roleName"><span
                  class="c8">角色：</span>{{ row.roleName }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="编辑" width="180px">
          <template #default="{ row }">
            <span class="w f-s-w" v-if="!row.isChildren">
              <el-button type="text" @click="appendTeam(row,true)" class="m-l6 c-s">编辑团队</el-button>
              <el-button type="text" @click="deleteTeam(row)" class="c-d">删除团队</el-button>
              <el-button type="text" @click="appendPerson({teamId:row.id})">添加成员</el-button>
            </span>
            <span v-else>
              <el-button type="text" @click="appendPerson(row,true)">编辑成员</el-button>
              <el-button type="text" @click="deleteTeamPerson(row)" class="c-w">删除成员</el-button>
            </span>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script lang="ts">
import frameProperty from '@/mixins/frame-property'
import { ElIconRender } from '@/importComponents'
import { RequestCode } from '@/server/base'
import { Team } from '@/apis/team/index'

export default {
  name: '菜单设置',
  mixins: [frameProperty],
  components: {
    ElIconRender
  },
  data() {
    return {
      // routerTable: this.$router.options.routes.filter(
      //   (item: { name?: string }) => item.name && item.name != 'redirected'
      // ),
      tableData: [],

      listLoading: false
    }
  },
  methods: {
    getMetaParams(meta: any) {
      if (!meta) {
        return {}
      }
      const { label, customLabel, hidden, width, handler, elIcon } = meta
      return {
        label,
        elIcon,
        customLabel: `<span>${customLabel ? '是' : '否'}</span>`,
        hidden: `<span>${hidden ? '是' : '否'}</span>`,
        width: `<span>${width}</span>`,
        handler: `<span>${handler}</span>`
      }
    },

    getDataList() {
      this.listLoading = true
      Team.list()
        .then((res: RequestCode) => {
          this.tableData = this.getTreeTeam(res.data)
          this.listLoading = false
        })
        .catch(() => {
          this.listLoading = false
        })
    },

    getTreeTeam(data: any) {
      let children = data.map(
        (
          item: {
            id: string | number
            name: string
            explain: string
            children?: any[]
            type: string

            uniId?: string //唯一编码
          },
          index: number
        ) => {
          this.getPersonByTeam(item.id, (result: any[]) => {
            ;(this.tableData[index] as any).children = (result || [])
              .filter(item => item)
              .map(
                (
                  ch: { uniId?: string; isChildren: boolean; teamId?: string | number },
                  chIndex: number
                ) => {
                  if (!ch) {
                    return ch
                  }
                  ch.uniId = `${item.id}_${chIndex}`
                  ch.isChildren = true
                  ch.teamId = item.id
                  return ch
                }
              )
          })
          item.uniId = item.id + ''
          return item
        }
      )
      return children
    },

    getPersonByTeam(teamId: string | number, fn: Function) {
      Team.getPersonById(teamId).then((res: RequestCode) => {
        if (typeof fn === 'function') {
          fn(res.data)
        }
      })
    },

    appendTeam(row: any = {}, isEdit: boolean = false) {
      this.dialogPagePush({
        name: 'teamEdit',
        params: {
          isEdit,
          row
        }
      })
    },
    appendPerson(row: any, isEdit: boolean = false) {
      this.dialogPagePush({
        name: 'teamPersonEdit',
        params: {
          isEdit: isEdit,
          row
        }
      })
    },

    deleteTeam(row: { id: number | string }) {
      if (!row.id) {
        return
      }
      this.$confirm('确定删除吗,删除后不可恢复！', '提示')
        .then(res => {
          Team.removeById({ id: row.id }).then((result: RequestCode) => {
            this.getDataList()
          })
        })
        .catch(e => {})
    },
    deleteTeamPerson(row: { id: number | string; teamId: number | string }) {
      if (!row.id) {
        return
      }
      this.$confirm('确定删除该成员吗', '提示')
        .then(res => {
          Team.removePersonById({ id: row.id, teamId: row.teamId }).then((result: RequestCode) => {
            this.getDataList()
          })
        })
        .catch(e => {})
    }
  },
  created() {
    this.getDataList()
  }
}
</script>

<style lang="scss" scoped></style>